package org.itboys.mobile.service.mysql.web.income;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mdl.conmon.ErroException;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.income.MdlIncomeCashedMapper;
import org.itboys.mobile.dao.income.MdlIncomeDetailMapper;
import org.itboys.mobile.entity.mysql.income.MdlIncome;
import org.itboys.mobile.entity.mysql.income.MdlIncomeCashed;
import org.itboys.mobile.entity.mysql.income.MdlIncomeDetail;
import org.itboys.mongodb.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/12/25
 * 描述：迈迪龙_后台_收益
 */
@Service
public class MdlWebIncomeService extends BaseMySqlService<MdlIncome>{
    @Autowired
    private MdlIncomeCashedMapper cashedMapper;
    @Autowired
    private MdlIncomeDetailMapper detailMapper;
    /**
     * 获取用户收益主表
     * @param memberId
     * @return
     */
    @Transactional
    public MdlIncome getIncome(Long memberId){
        //Map<String,Object> result = new HashMap<String, Object>();
        Map<String,Object> param = new HashMap<String, Object>();
        param.put("memberId", memberId);
        MdlIncome income=null;
        List<MdlIncome> incomes=super.select(param);
        if(ListUtil.isNotNull(incomes)){
            income=incomes.get(0);
        }else{
            income = new MdlIncome(memberId);
            super.insert(income);
            income =  super.find(income.getId());
        }
        return income;
    }
    /**
     * 获取提现记录
     * @param request
     * @param memberId
     * @param status
     * @return
     */
    public PageResult<MdlIncomeCashed> getIncomeCashed(HttpServletRequest request, Long memberId,Integer status){
        Map<String,Object> param = ParamUtil.packageMysqlPageParam(request, "name");
        if(LongUtil.isNotZreo(memberId)){
            param.put("memberId", memberId);
        }
        if(status!=null&&status!=0){
            param.put("status", status);
        }
        Long count=0l;
        Serializable o=cashedMapper.count(param);
        if(o!=null){
            count=Long.valueOf(o.toString());
        }
        return new PageResult<MdlIncomeCashed>(
                cashedMapper.select(param),
                count);


    }

    /**
     * 获取收益明细
     * @param request
     * @param memberId
     * @return
     */
    public PageResult<MdlIncomeDetail> getIncomeRecord(HttpServletRequest request, Long memberId){
        Map<String,Object> param = ParamUtil.packageMysqlPageParam(request, "");
        if(LongUtil.isNotZreo(memberId)){
            param.put("memberId", memberId);
        }
        Long count=0l;
        Serializable o=detailMapper.count(param);
        if(o!=null){
            count=Long.valueOf(o.toString());
        }
        return new PageResult<MdlIncomeDetail>(
                detailMapper.select(param),
                count);
    }

    /**
     * 同意提现
      * @param cashedId
     * @param payNo
     */
    @Transactional
    public void agreeApply(Long cashedId, String payNo){

        MdlIncomeCashed cashed=cashedMapper.find(cashedId);
        if (cashed.getStatus() != SundayCommonConstants.INCOME_YSQ) {
            throw new ErroException("请勿重复操作");
        }
        cashed.setPayNo(payNo);
        cashed.setStatus(SundayCommonConstants.INCOME_YTG);
        //cashed.setRemark("已经处理");
        cashedMapper.update(cashed);
    }

    /**
     * 拒绝提现
     * @param cashedId
     * @param remark
     */
    @Transactional
    public void disAgreeApply(Long cashedId,String remark){

        MdlIncomeCashed cashed=cashedMapper.find(cashedId);
        if (cashed.getStatus() != SundayCommonConstants.INCOME_YSQ) {
            throw new ErroException("请勿重复操作");
        }
        cashed.setRemark(remark);
        cashed.setStatus(SundayCommonConstants.INCOME_YBH);
        cashedMapper.update(cashed);
        //回滚提现金额
        MdlIncome income=getIncome(cashed.getMemberId());
        income.setAbleRevenue(income.getAbleRevenue().add(cashed.getApplyAmount()));
        super.update(income);

    }
}
